<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * User: kang
 * Date: 16/2/23
 * Time: 18:13
 */
class Plug_Param implements Plug_Base
{
    private static $instance=null;
    private static $request=null;

    public static function instance(Request $request)
    {
        if(is_null(self::$instance)){
            self::$instance = new self();
            self::$request = $request;
        }
        return self::$instance;
    }

    public function run()
    {
        $config = Kohana::$config->load ( 'param' )->as_array ();
        $request_path = self::$request->controller().'/'.self::$request->action();
        $request_path = strtolower($request_path);
        //请求api不需要验证直接跳过
        if ( !key_exists ( $request_path , $config )) {
            return true;
        }

        try {
            //提交方式非post错误
            if (self::$request->method() !== "POST") {
                throw new Exception("method is not post ,it is".self::$request->method());
            }

            foreach ($config[ $request_path ] as $param => $rules)
            {
                //参数不在post中错误
                $postParam = self::$request->post ( $param );
                if (is_null ( $postParam )) {
                    throw new Exception($param." not exist in post");
                }
                //规则不通过错误
                foreach ($rules as $method => $value)
                {
                    if(!method_exists($this,$method))
                    {
                        throw new Exception($method." not in param.php");
                    }
                    if( !self::$method($postParam, $value) )
                    {
                        throw new Exception($param.":".$postParam.' not pass '.$method);
                    }
                }
            }

            return true;
        }catch(Exception $e)
        {
            Log::instance()->add(Log::ERROR,"Plub_Param:".$request_path. ' ' .$e->getMessage());
            return false;
        }
    }


    //验证手机
    public static function phone($value)
    {
        return !preg_match("/^1[0-9]{10}$/",$value) ? false:true;
    }

    //验证是否为空  false空  true不空
    public static function not_empty($value)
    {
        return empty($value)&&$value!='0' ? false:true;
    }
    //验证最小长度
    public static function min_length($value,$length)
    {
        return Public_Helper::utf8_strlen($value)<$length ? false:true;
    }
    //验证最大长度
    public static function max_length($value,$length)
    {
        return Public_Helper::utf8_strlen($value)>$length ? false:true;
    }

    //验证验证码
    public static function captcha($value)
    {
        return Captcha::valid($value) ? true:false;
    }

    //是否是数字
    public static function isNumeric($value)
    {
        return is_numeric($value) ? true:false;
    }

    //只能在其中
    public static function isExist($value, array $list)
    {
        return in_array($value,$list) ? true:false;
    }

    //验证身份证
    public static function id_valid($value)
    {
        $preg = "/(^\d{15}$)|(^\d{17}([0-9]|[xX])$)/";
        preg_match($preg,$value,$match);
        return empty($match)? false:true;
    }



}

